package com.unigator;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.AsyncTask;

public abstract class ServerIO extends AsyncTask
{
	private ArrayList<MyLocation> locationList;
	private HashMap<String,Integer> pointList;
<<<<<<< HEAD

=======
	private boolean success;
	private int id;
>>>>>>> 9a456f07899b24566de210037bc575d047e37e32
	private eFunc func;
	
	public enum eFunc 
	{
<<<<<<< HEAD
		FindPathInt,FindPathDouble,FindPointByName
=======
		FindPathInt,FindPathDouble,FindPointByName,AddVertex,AddEdge
>>>>>>> 9a456f07899b24566de210037bc575d047e37e32
	}
	

	public class RetObject 
	{
		public eFunc func;
		public Object retData;
	}
	
	public ServerIO()
	{
		super();
		locationList=new ArrayList<MyLocation>();
		pointList=new HashMap<String,Integer>() ;
	}

	private void callServer(String funcName,String funcParam)
	{
		HttpClient httpClient = new DefaultHttpClient();
	    HttpContext localContext = new BasicHttpContext();
	   
	    HttpGet httpGet = new HttpGet("http://54.201.75.6/unigator/App.svc//"+funcName+"/?" + funcParam);
        String text = null;
        try 
        {
        	HttpResponse response = httpClient.execute(httpGet, localContext);
            HttpEntity entity = response.getEntity();
            text = getASCIIContentFromEntity(entity);
        } 
        catch (Exception e) 
        {
           	//TODO: add massage to user
        }

        handleResponse(text);
	}
	
	private ArrayList<MyLocation> findPath(int from,int to)
	{
		String funcParam="from="+from+"&to="+to;
		String funcName="findPathFromTo";
		
		callServer(funcName,funcParam);
        
        return locationList;
	}
	
	private ArrayList<MyLocation> findPath(double fromLat,double fromLong,int to)
	{	
		String funcParam="fromLat="+fromLat+"&fromLong"+fromLong+"&to="+to;
		String funcName="findPathFromTo";
		
		callServer(funcName,funcParam);
        
		return locationList;
	}
	
	private HashMap<String,Integer> findPoint(String name)
	{
		String funcParam="name="+name;
		String funcName="FindPointByName";
		
		callServer(funcName,funcParam);
        
        return pointList;
	}
	
<<<<<<< HEAD
=======
	private int addVertex(double latitude,double longitude)
	{
		String funcParam="longtitude="+longitude+"latitude"+latitude;
		String funcName="AddVertex";
		
		callServer(funcName,funcParam);
        
        return id;
	}
	
>>>>>>> 9a456f07899b24566de210037bc575d047e37e32
	protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException 
	{
		InputStream in = entity.getContent();
	
		StringBuffer out = new StringBuffer();
		int n = 1;
		while (n>0) 
		{
			byte[] b = new byte[4096];
			n =  in.read(b);
			if (n>0) out.append(new String(b, 0, n));
		}
	
		return out.toString();
	}
	
	@Override
	protected Object doInBackground(Object... params) 
	{
		func=(eFunc)params[0];
		Object retVal=null;
		RetObject retObject=new RetObject();
		retObject.func=func;
		switch(func)
		{
			case FindPathInt:
			{
				int from=(Integer)params[1];
				int to =(Integer)params[2];
				
				retObject.retData= findPath(from, to);
				
				break;
			}
			case FindPathDouble:
			{
				double fromLat=(Double)params[1];
				double fromLong=(Double)params[2];	
				int to =(Integer)params[3];
				
				retObject.retData= findPath(fromLat,fromLong, to);
				
				break;
			}
			case FindPointByName:
			{
		        retObject.retData= findPoint((String)params[1]);
<<<<<<< HEAD
		        
		        break;
			}
=======
		        break;
			}
			case AddVertex:
			{
				double latitude=(Double)params[1];
				double longitude=(Double)params[2];	
				
				retObject.retData= addVertex(latitude,longitude);
				
				break;
			}
			case AddEdge:
			{
				retObject.retData=1;
			}
>>>>>>> 9a456f07899b24566de210037bc575d047e37e32
			default:
			{
				break;
			}
		}
		
		retVal=retObject;
		return retVal;
	}
	     
	public void handleResponse(String text) 
    {
		switch(func)
		{
			case FindPathInt:
			{
				locationList.clear();
		    	try 
		    	{
		            JSONArray jsonArray = new JSONArray(text);
		            for (int i = 0; i < jsonArray.length(); i++) 
		            {
		            	JSONObject jsonObject = jsonArray.getJSONObject(i);
		            	MyLocation newLocation=new MyLocation(jsonObject.getDouble("Longtitude"),jsonObject.getDouble("Latitude"));
		            	locationList.add(newLocation);
		            }
		        } 
		    	catch (Exception e) 
		    	{
		    		//TODO add massage to user??
		            e.printStackTrace();
		        }
				break;
			}
			case FindPathDouble:
			{
				locationList.clear();
		    	try 
		    	{
		            JSONArray jsonArray = new JSONArray(text);
		            for (int i = 0; i < jsonArray.length(); i++) 
		            {
		            	JSONObject jsonObject = jsonArray.getJSONObject(i);
		            	MyLocation newLocation=new MyLocation(jsonObject.getDouble("Longtitude"),jsonObject.getDouble("Latitude"));
		            	locationList.add(newLocation);
		            }
		        } 
		    	catch (Exception e) 
		    	{
		    		//TODO add massage to user??
		            e.printStackTrace();
		        }
				break;
			}
			case FindPointByName:
			{
				pointList.clear();
				try 
		    	{
		            JSONArray jsonArray = new JSONArray(text);
		            for (int i = 0; i < jsonArray.length(); i++) 
		            {
		            	JSONObject jsonObject = jsonArray.getJSONObject(i);
		            	pointList.put(jsonObject.getString("Name"),jsonObject.getInt("Id"));
		            }
		        } 
		    	catch (Exception e) 
		    	{
		    		//TODO add massage to user??
		            e.printStackTrace();
		        }
				break;
			}
<<<<<<< HEAD
=======
			case AddVertex:
			{
				pointList.clear();
				try 
		    	{
		            JSONArray jsonArray = new JSONArray(text);
		            for (int i = 0; i < jsonArray.length(); i++) 
		            {
		            	JSONObject jsonObject = jsonArray.getJSONObject(i);
		            	id=jsonObject.getInt("Id");//check that
		            }
		        } 
		    	catch (Exception e) 
		    	{
		    		//TODO add massage to user??
		            e.printStackTrace();
		        }
				break;
			}
>>>>>>> 9a456f07899b24566de210037bc575d047e37e32
		}
		
    }
	
}
